<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

<script>



    // 箭头函数
    var fn = async v => v;
    var fn = async (v) => v;
    var result = fn(1);
    console.log(result);


    var a = getNumAsync(1, 2000)
    var b = getNumAsync(2, 3000)
    var c = getNumAsync(3, 1000)

    // var p = async function () {
    //     var aa = await a
    //     console.log(aa);
    //     var bb = await b
    //     console.log(bb);
    //     var cc = await c
    //     console.log(cc);
    //     return aa + bb + cc
    // }
    // p()
    // console.log(p());




    async function lg() {
        var aa = await getNumAsync(1, 2000)
        console.log(aa);
        var bb = await getNumAsync(2, 3000)
        console.log(bb);
        var cc = await getNumAsync(3, 1000)
        console.log(cc);
        return aa + bb + cc
    }


    lg().then(res => {
        console.log(res);
    }).catch(err => {
        console.log(err);
    })




    // var p1 = getNumAsync(1, 2000); // 返回Promise实例  2s后 =>{<fulfilled>:1}
    // var p2 = getNumAsync(2, 1000); // 返回Promise实例  1s后 =>{<fulfilled>:2}
    // var p3 = getNumAsync(3, 3000); // 返回Promise实例  3s后 =>{<fulfilled>:3}

    // async function run() {
    //     // debugger;
    //     var a = await p1;
    //     console.log(a);
    //     var b = await p2;
    //     console.log(b);
    //     var c = await p3;
    //     console.log(c);
    //     return a + b + c;
    // }


    // var p = run();
    // p.then(res => {
    //     console.log(res);
    // }).catch(err => {
    //     console.log(err);
    // })







    function getNumAsync(num = 0, delay = 0, isFulfilled = true) {
        return new Promise(function (resolve, reject) { // 形参
            setTimeout(function () {
                isFulfilled ? resolve(num) : reject(num);
            }, delay)
        });
        // 返回Promise对象  {<pending>:undefined}
    }
</script>

</html>